package com.wzy.rabbitmqapi.quickstart;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

import java.io.IOException;

/**
 * 固定写法
 */
public class MyConsumer extends DefaultConsumer {
    private Channel channel;
    //重写构造函数,Channel通道对象需要从外层传入,在handleDelivery中要使用
    public MyConsumer(Channel channel) {
        super(channel);
        this.channel=channel;
    }
    //
    @Override
    public void handleDelivery(String s, Envelope envelope, AMQP.BasicProperties basicProperties, byte[] bytes) throws IOException {
        System.out.println("---------consume message--------------");
//        System.out.println("consumerTag:" + s);
//        System.out.println("envelope:" + envelope);
//        System.out.println("properties:" + basicProperties);
        //消费者获取到的消息
        System.out.println("body:" + new String(bytes));
        //签收消息,确认消息
        //envelope.getDeliveryTag(),获取这个消息的TagId
        //false 只确认签收当前消息; true:代表签收该消费者所有未签收的消息
        channel.basicAck(envelope.getDeliveryTag(),false);
    }
}
